package com.linkzd.app.controller;

import com.linkzd.app.model.DeptModel;
import com.linkzd.app.model.DeviceModel;
import com.linkzd.app.model.DeviceResult;
import com.linkzd.app.service.IDeviceInfoOfRoomService;
import com.linkzd.common.annotation.Log;
import com.linkzd.common.core.controller.BaseController;
import com.linkzd.common.core.domain.AjaxResult;
import com.linkzd.common.core.domain.entity.SysDept;
import com.linkzd.common.enums.BusinessType;
import com.linkzd.iot.domain.DeviceInfo;
import com.linkzd.iot.domain.RoomInfo;
import com.linkzd.iot.service.IDeviceInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import static org.apache.commons.lang3.StringUtils.isNumeric;

/**
 * @author: Wangym
 * @Date: 2024/5/23 16:01
 * @Description:
 */
@RestController
@RequestMapping("/app/deviceInfo")
public class DeviceInfoOfRoomController extends BaseController {

    @Autowired
    private IDeviceInfoOfRoomService deviceInfoOfRoomService;

    /**
     * 根据房间号查询设备信息列表
     */
    @GetMapping("/list")
    public AjaxResult list(RoomInfo roomInfo)
    {
        if(roomInfo.getRoomNum() != null && roomInfo.getRoomNum() != ""){
            List<SysDept> sysDeptList = deviceInfoOfRoomService.selectRoomInfoList();
            DeviceResult list = null;
            for (SysDept sysDept:sysDeptList){
                if(roomInfo.getRoomNum().equals(sysDept.getDeptName())){
                    list = deviceInfoOfRoomService.selectDeviceInfoList(roomInfo);
                }
            }
            if(list != null){
                return new AjaxResult(200,null,list);
            }
            return new AjaxResult(500,"您没有该" + roomInfo.getRoomNum() + "的访问权限!",null);
        }
        return new AjaxResult(500,"房间号不能为空!",null);
    }

    /**
     * @Description: TODO 根据用户查询用户的房间
     * @Author: cyx
     * @Date: 2024/6/12 9:41
     */
    @GetMapping("/roomlist")
    public AjaxResult deptNameList()
    {
        List<SysDept> sysDepts = deviceInfoOfRoomService.selectRoomInfoList();
        List<DeptModel> list = new ArrayList<>();
        for (SysDept sDept:sysDepts) {
            list.add( new DeptModel(Long.valueOf(sDept.getDeptName()),sDept.getDeptName()));
        }
        return new AjaxResult(200,null,list);
    }

    /**
     * 大棚根据房间号查询设备信息列表
     */
    @GetMapping("/allList")
    public AjaxResult allRoomInfoList(RoomInfo roomInfo)
    {
        if(roomInfo.getRoomNum() != null && roomInfo.getRoomNum() != ""){
            List<SysDept> sysDeptList = deviceInfoOfRoomService.selectRoomInfoList();
            com.linkzd.iot.model.DeviceResult list = null;
            for (SysDept sysDept:sysDeptList){
                if(roomInfo.getRoomNum().equals(sysDept.getDeptName())){
                    list = deviceInfoOfRoomService.selectDeviceInfoAllList(roomInfo);
                }
            }
            if(list != null){
                return new AjaxResult(200,null,list);
            }
            return new AjaxResult(500,"您没有该" + roomInfo.getRoomNum() + "的访问权限!",null);
        }
        return new AjaxResult(500,"房间号不能为空!",null);
    }
}

